package cn.david.pet.service.impl;


import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.pet.domain.Pet;
import cn.david.pet.domain.PetDetail;
import cn.david.pet.mapper.PetDetailMapper;
import cn.david.pet.mapper.PetMapper;
import cn.david.pet.service.IPetService;
import cn.david.user.domian.User;
import cn.david.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PetServiceImpl  extends BasicServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        if (detail!=null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);

        }

    }

    @Override
    public void onSale(Long[] ids) {
        petMapper.onsale(ids);
    }

    @Override
    public PetDetail loadByIdDetail(Long id) {
        return petDetailMapper.loadByIdDetail(id);
    }

    @Override
    public void fnSale(Long[] ids) {
        petMapper.fnSale(ids);

    }

    @Override
    public void adpot(Long id, Long loginInfoId) {
        Pet pet = petMapper.loadById(id);
        if (pet!=null){
            pet.setState(0);
            pet.setOffsaletime(new Date());
            //绑定领养人
            User user = userMapper.loadByLogininfoId(loginInfoId);
            pet.setUser_id(user.getId());
            petMapper.update(pet);
        }
    }
}
